package com.cn.longdistancebusstation.calendar;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


import com.cn.longdistancebusstation.R;
import com.cn.longdistancebusstation.base.BaseActivity;
import com.othershe.calendarview.bean.DateBean;
import com.othershe.calendarview.listener.CalendarViewAdapter;
import com.othershe.calendarview.listener.OnPagerChangeListener;
import com.othershe.calendarview.listener.OnSingleChooseListener;
import com.othershe.calendarview.utils.CalendarUtil;
import com.othershe.calendarview.weiget.CalendarView;

import org.xutils.common.util.LogUtil;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by mucheng on 2018/1/26.
 */

public class CalendarActivity extends BaseActivity implements View.OnClickListener{
    private CalendarView mCalendarView;
    private TextView mChooseDateTextView;
    private int[] cDate = CalendarUtil.getCurrentDate();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calendar);
        getTitleTextView().setText("日期");
        final TextView title = (TextView) findViewById(R.id.title);
        //当前选中的日期
        mChooseDateTextView = findViewById(R.id.choose_date);
        //
        Intent intent = getIntent();
        String outingDate = intent.getStringExtra("outingDate");
        String[] dateComponents = outingDate.split("-");
        title.setText(dateComponents[0]+"年"+Integer.parseInt(dateComponents[1])+"月");
        //
        mCalendarView = (CalendarView) findViewById(R.id.calendar);
        Calendar calendar = Calendar.getInstance();
        Date startDate = calendar.getTime();
        DateFormat dateFormat = new SimpleDateFormat("yyyy.M");
        calendar.add(Calendar.MONTH, 2);
        Date endDate = calendar.getTime();
        String strStartDate = dateFormat.format(startDate);
        String strEndDate = dateFormat.format(endDate);
        String[] components = outingDate.split("-");
        mCalendarView.setStartEndDate(strStartDate, strEndDate)
                    .setInitDate(components[0] + "." + components[1])
                    .setSingleDate(components[0] + "." + components[1] + "." + components[2])
                    .init();
        title.setText(cDate[0] + "年" + cDate[1] + "月");
        mChooseDateTextView.setText("当前日期为：" + cDate[0] + "年" + cDate[1] + "月" + cDate[2] + "日");
        Log.d("时间1", mChooseDateTextView +"");
        mCalendarView.setOnPagerChangeListener(new OnPagerChangeListener() {
            @Override
            public void onPagerChanged(int[] date) {
                title.setText(date[0] + "年" + date[1] + "月");
            }
        });

        mCalendarView.setOnSingleChooseListener(new OnSingleChooseListener() {
            @Override
            public void onSingleChoose(View view, DateBean date) {
                title.setText(date.getSolar()[0] + "年" + date.getSolar()[1] + "月");
                mChooseDateTextView.setText(date.getSolar()[0] + "年" + date.getSolar()[1] + "月" + date.getSolar()[2] + "日");
                int year = date.getSolar()[0];
                int month = date.getSolar()[1];
                int day = date.getSolar()[2];
                Intent intent = getIntent();
                intent.putExtra("outingDate", year+"-"+(month<10?"0"+month:month)+"-"+(day<10?"0"+day:day));
                setResult(RESULT_OK, intent);
                finish();
            }
        });

    }

    public void someday(View v) {
        View view = LayoutInflater.from(CalendarActivity.this).inflate(R.layout.input_layout, null);
        final EditText year = (EditText) view.findViewById(R.id.year);
        final EditText month = (EditText) view.findViewById(R.id.month);
        final EditText day = (EditText) view.findViewById(R.id.day);

        new AlertDialog.Builder(this)
                .setView(view)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (TextUtils.isEmpty(year.getText())
                                || TextUtils.isEmpty(month.getText())
                                || TextUtils.isEmpty(day.getText())) {
                            Toast.makeText(CalendarActivity.this, "请完善日期！", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        boolean result = mCalendarView.toSpecifyDate(Integer.valueOf(year.getText().toString()),
                                Integer.valueOf(month.getText().toString()),
                                Integer.valueOf(day.getText().toString()));
                        if (!result) {
                            Toast.makeText(CalendarActivity.this, "日期越界！", Toast.LENGTH_SHORT).show();
                        } else {
                            mChooseDateTextView.setText("当前选中的日期为：" + year.getText() + "年" + month.getText() + "月" + day.getText() + "日");
                            Log.d("时间2", mChooseDateTextView +"");
                        }
                        dialog.dismiss();
                    }
                })
                .setNegativeButton("取消", null).show();
    }

    public void today(View view) {
        mCalendarView.today();
        mChooseDateTextView.setText("当前选中的日期为：" + cDate[0] + "年" + cDate[1] + "月" + cDate[2] + "日");
        Log.d("时间3", mChooseDateTextView +"");
    }

    public void lastMonth(View view) {
        mCalendarView.lastMonth();
    }

    public void nextMonth(View view) {

        mCalendarView.nextMonth();
    }

    public void start(View view) {
        mCalendarView.toStart();
    }

    public void end(View view) {
        mCalendarView.toEnd();
    }

    public void lastYear(View view) {
        mCalendarView.lastYear();
    }

    public void nextYear(View view) {
        mCalendarView.nextYear();
    }

    public void multiChoose(View view) {
        startActivity(new Intent(CalendarActivity.this, MultiChooseActivity.class));
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
        }

    }

}
